// pages/keepcar/carsrvdetail.js
var util = require('../../utils/util.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bannerUrl: '',
    orderid: 0,
    orderinfo: [],
    isshow: true,
    userid:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      orderid: options.oid,
      userid: wx.getStorageSync('userId')
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getOrderdetail();
    this.initBanner();
  },
  //广告图片初始化
  initBanner: function () {
    var that = this;
    var requrl = app.globalData.hostUrl + 'common/banner';
    var imgbanner = wx.getStorageSync('keepBanner');
    //console.log(imgbanner.length);
    if (!app.expiratTime('keepBanner') || imgbanner.length == 0) {
      wx.request({
        url: requrl,
        data: { 'location': '302' },
        header: {
          'grant-appid': app.globalData.appid
        },
        success: function (res) {
          if (res.statusCode === 200) {
            wx.setStorageSync('keepBanner', res.data.result[0].imgpath);
            that.setData({
              bannerUrl: res.data.result[0].imgpath
            })
          }
        }
      })
    }
    else {
      that.setData({
        bannerUrl: imgbanner
      })
    }
  },
  
  getOrderdetail: function () {
    var that = this;
    var requrl = app.globalData.hostUrl + 'handler/carsvretail';
    wx.request({
      url: requrl,
      data: { 'oid': that.data.orderid },
      header: {
        'grant-appid': app.globalData.appid
      },
      success: function (res) {
         console.log(res);
        if (res.data.code == 200) {
          var orderinfo = res.data.result;
          var _isshow = true;
          if (orderinfo.status != 0) {
            _isshow = false;
          }
          that.setData({
            orderinfo: orderinfo,
            isshow: _isshow
          })
        }
      }
    })
  },
  confirmpay: function () {
    var that = this;
    var clickflag = 1;
    var postdata = {
      'openid': wx.getStorageSync('openId'),
      'uid': wx.getStorageSync('userId'),
      'oid': this.data.orderid,
      'flag': 'carsvr'
    };
    //console.log(postdata);
    var requrl = app.globalData.hostUrl + 'handler/orderPrepay';
    if (clickflag == 1) {
      clickflag = 0;
      wx.request({
        url: requrl,
        data: postdata,
        header: {
          'Content-Type': 'application/x-www-form-urlencoded',
          'grant-appid': app.globalData.appid
        },
        method: 'POST',
        success: function (res) {
          //console.log(res);
          if (res.data.code == 200) {
            wx.requestPayment({
              'timeStamp': res.data.result.timeStamp,
              'nonceStr': res.data.result.nonceStr,
              'package': res.data.result.package,
              'signType': 'MD5',
              'paySign': res.data.result.paySign,
              'success': function (res) {
                wx.showModal({
                  title: '提示',
                  content: '支付成功!',
                  success: function (res) {
                    wx.navigateTo({
                      url: '../keepcar/mycarsrv',
                    })
                  }
                });
              },
              'fail': function (res) {

              },
              'complete': function (res) {
                wx.navigateTo({
                  url: '../myself/mycarsrv',
                })
              }
            });

          }
          else {
            util.isError(res.data.result, that);
          }
          clickflag = 1;
        }
      })
    }
  }

})